﻿using EmoneyWeiXinCommon;
using EmoneyWeiXinDAL;
using EmoneyWeiXinEntity;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using tenpayApp;

namespace EmoneyWeiXinBLL
{
    public class HuaYuanDanBLL
    {

        public static object lockObj = new object();
        public static int AddChouJiangRec(string weixinid, string openid, int result)
        {
            return HuaYuanDanDAL.AddChouJiangRec(weixinid, openid, result);
        }

        public static string Tixian(string weixinId, string openid, SendRedPackConfig srpConfig, out decimal money)
        {
            //lock (lockObj)
            //{

              
                try
                {
                    string pre_mch_billno = DateTime.Now.ToString("HHmmss") + TenpayUtil.BuildRandomStr(10);
                    pre_mch_billno = srpConfig.mch_id + pre_mch_billno;
                    string t_order_no = pre_mch_billno;
                    string sendMessage = string.Empty;


                   int resut=  HuaYuanDanDAL.Tixian(weixinId, openid, t_order_no, out money);
                    if (money > 0)
                    {
                        string sendResult = WeiXinPayBLL.sendRedBag(srpConfig, openid, (double)money * 100, t_order_no, out sendMessage);
                        if (sendResult == "")
                        {
                            HuaYuanDanDAL.UpdateRec(weixinId, openid, t_order_no, money, 1);                            
                            return "提取成功！\n我们已以红包的形式发送给您,请回到公众号查看\n";
                        }
                        else
                        {
                           
                            //该用户今日领取红包个数超过限制,如有需要请登录微信支付商户平台更改API安全配置.
                            if (sendMessage.Contains("该用户今日领取红包个数超过限制"))
                            {
                                return "每日最多提现三次哦，明日再来吧";
                            }
                            else
                            {
                                HuaYuanDanDAL.UpdateRec(weixinId, openid, t_order_no, money, 0);
                                return "当前活动人数较多，稍后为您推送，请耐心等待！";
                            }

                        }
                    }
                    else if (money == -1003)
                    {
                        return "红包已经领走了";
                    }
                    else if (money == -1001)
                    {
                        return "请领取非红包大奖";
                    }
                    else if (money == -1002)
                    {
                        return "奖品已过期，请重新抽奖";
                    }
                    else {
                        return "";
                    }

                }
                catch (Exception ex)
                {
                    //tran.Rollback();
                    money = 0;
                    return "提现失败" + ex.Message;
                }
                //}                

            }

        //}
    }
}
